package com.etc.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class FileController {

    @RequestMapping("getInfo")
    public Map<String,Object> getInfo(String id){
        System.out.println(id);
        HashMap<String,Object> data = new HashMap<>();
        data.put("code", UUID.randomUUID().toString().replace("-",""));
        data.put("id", id);
        return data;
    }

    @RequestMapping("postData")
    public Map<String,Object> postData(@RequestBody Map<String,Object> param){
        System.out.println(param);
        HashMap<String,Object> data = new HashMap<>();
        data.put("code", UUID.randomUUID().toString().replace("-",""));
        return data;
    }

    @RequestMapping("upload")
    public Object upload(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        MultipartFile content = multiRequest.getFile("content");
        byte[] bytes = content.getBytes();
        String result = new String(bytes);
        return result;
    }


}
